// 引入SDK核心类
var QQMapWX = require('libs/qqmap-wx-jssdk.js');
var mapInstance = new QQMapWX({
  key: '2VNBZ-VD4K2-R6SU7-CDODD-4SOY7-FVFR7' // 必填
});
App({
  onLaunch: function () {
    console.log('App Launch')
  },
  onShow: function () {
    console.log('App Show');
  },
  onHide: function () {
    console.log('App Hide');
  },
  globalData: {
    hasLogin: false,
    hasDoLogin: false,
    hasLogout: false,
    located: false,
    identyTime: 0,
    isUpLoadLocation: true
  },

  //将定位方法公开出去
  startLocationListener: function () {
    this.globalData.located = true;
    startLocationListener(this);
  },

  stopLocationListener: function () {
    stopLocationListener(this);
  },

  getLocation: function () {
    getLocation();
  }
});

function startLocationListener(that) {
  console.log('locationLisent start...');
  if (!that.globalData.located || !that.globalData.hasLogout) {
    return;
  } else {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log(res);

        mapInstance.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            console.log(res);
            pushLocationInfo(res);
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
      }
    });

    setTimeout(function () {
      console.log("执行了这个settimeout")
      startLocationListener(that);
    }, 60000);
  }
}

function getLocation() {
  wx.getLocation({
    type: 'wgs84',
    success: function (res) {
      var latitude = res.latitude
      var longitude = res.longitude
      console.log(res);

      mapInstance.reverseGeocoder({
        location: {
          latitude: latitude,
          longitude: longitude
        },
        success: function (res) {
          console.log(res);
          pushLocationInfo(res);
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          console.log(res);
        }
      });
    }
  });
}

function stopLocationListener(that) {
  console.log('locationLisent is stoped');
  that.globalData.located = false;
}

function pushLocationInfo(res) {
  var username = wx.getStorageSync('username');
  wx.request({
    url: 'https://www.lanshuicar.com/Server/InsertGPS.ashx',
    data: {
      phone: username,
      jdu: res.result.location.lng,
      wdu: res.result.location.lat,
      city: res.result.ad_info.city,
      province: res.result.ad_info.province,
      district: res.result.ad_info.district,
      address: res.result.address,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
      console.log(res);
      var code = res.code;
      if (code == 0) {
      }
    },
    fail: function () {
      // fail
    },
    complete: function () {
      // complete
    }
  });
}